import 'package:flutter/material.dart';
import 'package:flutterproject/page/mine/children/assets/data/tong_piao_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:flutterproject/widget/text/text_field_common.dart';
import 'package:get/get.dart';

class TongPiao extends StatefulWidget {
  final int? walletId;

  const TongPiao({super.key, this.walletId});

  @override
  TongPiaoState createState() => TongPiaoState();
}

class TongPiaoState extends State<TongPiao> {
  int currentIndex = 0;

  List<TongPiaoBean?>? list = [];

  final TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
      alignment: Alignment.topCenter,
      child: Container(
        margin: MarginPaddingUtil.symmetric(horizontal: 40, vertical: 64),
        padding: MarginPaddingUtil.fromLTRB(
            left: 30, top: 102, right: 30, bottom: 58),
        decoration: BoxDecoration(
            color: Color(0xFFF9B57B),
            borderRadius: BorderRadius.circular(40.radius)),
        width: 640.width,
        height: 980.height,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: MarginPaddingUtil.fromLTRB(
                  left: 20, top: 40, right: 0, bottom: 32),
              decoration: BoxDecoration(
                  color: Color(0xFFF3AC6F),
                  borderRadius: BorderRadius.circular(20.radius)),
              child: Row(
                children: [
                  Expanded(
                    child: Row(
                      children: [
                        TextCommon(
                          "从",
                          size: 28.sp,
                          color: Colors.white.withOpacity(0.5),
                        ),
                        Expanded(
                          child: TextCommon(
                            "众商通",
                            size: 28.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            align: TextAlign.center,
                          ),
                        ),
                      ],
                    ),
                  ),
                  ImageCommon(
                    "/image/ic_change",
                    size: 48.width,
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        42.widthBox,
                        TextCommon(
                          "至",
                          size: 28.sp,
                          color: Colors.white.withOpacity(0.5),
                        ),
                        Expanded(
                          child: TextCommon(
                            "聚星同辉",
                            size: 28.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            align: TextAlign.center,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            50.heightBox,
            Container(
              height: 524.height,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.radius),
              ),
              child: Column(
                children: [
                  33.heightBox,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      27.widthBox,
                      SizedBox(
                        width: 45.width,
                        child: TextCommon(
                          "选择",
                          color: Color(0xFFDB8944),
                          size: 22.sp,
                          lineHeight: 28.sp,
                          align: TextAlign.center,
                        ),
                      ),
                      SizedBox(
                        width: 100.width,
                        child: TextCommon(
                          "商品名称",
                          color: Color(0xFFDB8944),
                          size: 22.sp,
                          lineHeight: 28.sp,
                          align: TextAlign.center,
                        ),
                      ),
                      SizedBox(
                        width: 100.width,
                        child: TextCommon(
                          "价格",
                          color: Color(0xFFDB8944),
                          size: 22.sp,
                          lineHeight: 28.sp,
                          align: TextAlign.center,
                        ),
                      ),
                      SizedBox(
                        width: 60.width,
                        child: TextCommon(
                          "数量",
                          color: Color(0xFFDB8944),
                          size: 22.sp,
                          lineHeight: 28.sp,
                          align: TextAlign.center,
                        ),
                      ),
                      SizedBox(
                        width: 96.width,
                        child: TextCommon(
                          "输入量",
                          color: Color(0xFFDB8944),
                          size: 22.sp,
                          lineHeight: 28.sp,
                          align: TextAlign.center,
                        ),
                      ),
                      29.widthBox,
                    ],
                  ),
                  10.heightBox,
                  Divider(
                    height: 1,
                    color: Color(0xFFE99A57),
                  ),
                  13.heightBox,
                  Expanded(
                    child: ListView.separated(
                      shrinkWrap: true,
                      padding: EdgeInsets.all(0),
                      itemCount: list?.length ?? 0,
                      itemBuilder: (BuildContext context, int index) {
                        TongPiaoBean? bean = list![index];
                        return Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            27.widthBox,
                            if (currentIndex != index)
                              Container(
                                margin: MarginPaddingUtil.all(6),
                                width: 33.width,
                                height: 33.width,
                                decoration: BoxDecoration(
                                  color: Color(0xFFF6F6F6),
                                  border: Border.all(
                                    color: Color(0xFFF9B57B),
                                    width: 1.width,
                                  ),
                                  shape: BoxShape.circle,
                                ),
                              ),
                            if (currentIndex == index)
                              Container(
                                margin: MarginPaddingUtil.all(6),
                                width: 33.width,
                                height: 33.width,
                                child: ImageCommon(
                                  "/image/ic_right",
                                  size: 33.width,
                                ),
                              ),
                            SizedBox(
                              width: 100.width,
                              child: TextCommon(
                                bean?.wareName ?? "",
                                color: Color(0xFFDB8944),
                                size: 24.sp,
                                lineHeight: 28.sp,
                                align: TextAlign.center,
                              ),
                            ),
                            SizedBox(
                              width: 100.width,
                              child: TextCommon(
                                "${bean?.price ?? 0.00}",
                                color: Color(0xFFDB8944),
                                size: 24.sp,
                                lineHeight: 28.sp,
                                align: TextAlign.center,
                              ),
                            ),
                            SizedBox(
                              width: 60.width,
                              child: TextCommon(
                                "${bean?.canPickNum ?? 0}",
                                color: Color(0xFFDB8944),
                                size: 24.sp,
                                lineHeight: 28.sp,
                                align: TextAlign.center,
                              ),
                            ),
                            if (currentIndex != index)
                              SizedBox(
                                width: 96.width,
                                height: 42.height,
                              ),
                            if (currentIndex == index)
                              Container(
                                width: 96.width,
                                height: 42.height,
                                decoration: BoxDecoration(
                                  color: Color(0xFFF6F6F6),
                                  borderRadius:
                                      BorderRadius.circular(12.radius),
                                ),
                                child: TextFieldCommon(
                                  width: 96.width,
                                  align: TextAlign.center,
                                  controller: _textEditingController,
                                  keyboardType: TextInputType.number,
                                ),
                              ),
                            29.widthBox,
                          ],
                        ).addClickEvent(() {
                          currentIndex = index;
                          setState(() {});
                        });
                      },
                      separatorBuilder: (BuildContext context, int index) =>
                          16.heightBox,
                    ),
                  ),
                  33.heightBox,
                ],
              ),
            ),
            40.heightBox,
            ButtonCommon(
              text: "确定充值",
              onTap: () => showPwdDialog(),
              width: 472.width,
              color: Colors.white,
              borderRadios: 30.radius,
              fontColor: Color(0xFFEDC88B),
              fontSize: 36.sp,
              fontWeight: FontWeight.bold,
              padding: MarginPaddingUtil.symmetric(vertical: 20),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    getList();
  }

  HttpClient client = Get.find<HttpClient>();

  /// 充值
  Future<bool> getList() async {
    try {
      DialogUtil.showLoading();
      list = await client.get<List<TongPiaoBean?>?>(AddressManager.tongPiao);
      setState(() {});
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  /// 充值
  Future charge(String pwd) async {
    try {
      DialogUtil.showLoading();
      await client.post(
        AddressManager.tongPiaoCharge,
        data: {
          "contNo": list?[currentIndex]?.contNo,
          "fdate": list?[currentIndex]?.fdate,
          "password": pwd,
          "value": _textEditingController.text,
          "walletId": widget.walletId
        },
      );
      ToastUtil.showSuccess("充值成功!");
      Get.back();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  void showPwdDialog() {
    DialogUtil.showPwdDialog((value) {
      charge(value);
    });
  }
}
